{extend name="index/public" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <div class="layui-inline">
        <div class="layui-input-inline">
            <input type="text" class="layui-input" id="interface" placeholder="接口地址">
        </div>
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="tableBar">
    {{#  if(d.pri_name !== '顶级权限'){ }}
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add">添加</a>
    <a class="layui-btn layui-btn-green layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
    {{#  } else { }}

    {{#  } }}
</script>
{/block}

{block name="css"}
{/block}

{block name="js"}
<script>
layui.use('table', function(){
    var table = layui.table
        ,$ = layui.$
        ,form = layui.form
        ,load = null
        ,active = {
            reload: function () {
                var interface = $('#interface').val();

                table.reload('tableId', {
                    page: {curr: 1}
                    ,where: {interface:interface}
                    ,height: 'full-100'
                })
            },
        };

    //表格初始化
    table.render({
        elem: '#tableBox'
        ,height: 'full-100'
        ,url: '{:url("SystemPrivilegeNew/index")}'
        ,page: true
        ,limit:20
        ,limits:[20,40,60,80]
        ,loading:true
        ,id:'tableId'
        ,done: function (res, curr, count) {
            layer.close(load);
            $('#layui-loading').hide();
        }
        ,cols: [[
            {type:'checkbox', fixed: 'left'}
            ,{field:'id', title: 'ID', width: 60, sort: true}
            ,{field:'pid', title: '父级ID', width: 200, sort: true}
            ,{field:'pri_name', title: '权限名称', width: 200, sort: true}
            ,{field:'url', title: '接口地址', width: 200, sort: true}
            ,{field:'rid', title: '二级菜单', width: 200, sort: true}
            ,{title: '操作', width: 300, align:'center', toolbar:'#tableBar'}
        ]]
    });

    //监听搜索
    $('#interface').blur(function(){
        load = layer.load();
        active['reload'].call(this);
    })

    //监听工具条
    table.on('tool(tableBox)', function(obj){
        var data = obj.data;
        if(obj.event === 'add'){
            layer.open({
                type: 2
                ,title: '添加'
                ,resize: true
                ,area: ['100%', '100%']
                ,content: '{:url("SystemPrivilegeNew/add")}?id='+data.id
            });
        }else if(obj.event === 'edit'){
            layer.open({
                type: 2
                ,title: '编辑'
                ,resize: true
                ,area: ['100%', '100%']
                ,content: '{:url("SystemPrivilegeNew/edit")}?id='+data.id
                ,cancel: function(){
                    $.post('{:url("SystemPrivilegeNew/index")}',{id: data.id}, function(json){
                        obj.update({
                            pri_name: json.data[0].pri_name
                            ,url: json.data[0].url
                            ,rid: json.data[0].rid
                        })
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                }
            });
        }else if(obj.event === 'del'){
            layer.confirm('确定要删除吗？', {offset: '250px'}, function(index){
                var load = layer.load();
                $.post('{:url("SystemPrivilegeNew/del")}',{id: data.id}, function(json){
                    layer.close(load);
                    if(json.code==0){
                        obj.del();
                        layer.msg(json.msg, {icon: 1, time:1000})
                    }else{
                        layer.msg(json.msg, {icon: 0, time:1000});
                    }
                }, 'JSON').error(function(){
                    layer.close(load);
                    layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                });
            });
        }
    });

});
</script>
{/block}